home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / sim.lha / sim / builtin / name.c < prev    next >
C/C++ Source or Header  |  1991-05-21  |  4KB  |  123 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* name.c */
  25.  
  26. #include "builtin.h"
  27.  
  28. LONG ptr;
  29. static BYTE perm = PERM;
  30.  
  31. extern LONG_PTR insert();
  32.  
  33. b_NAME0()  /* X, L: reg2 (L) is made to be the string of the name of reg1 (X) */
  34. {
  35.    PSC_REC_PTR       psc_ptr;
  36.    CHAR_PTR name;
  37.    LONG     i, len, nlist;
  38.    register LONG     op1;
  39.    register LONG_PTR top;
  40.    LONG_PTR stack_top;
  41.  
  42.    op1 = reg[1];  DEREF(op1);
  43.    if (!(ISCONSTR(op1) && GET_STR_ARITY(op1) == 0)) {
  44.       printf("Error: name0, illegal argument\n");
  45.       FAIL0;
  46.       return;
  47.    }
  48.    psc_ptr = GET_STR_PSC(op1);
  49.    name    = GET_NAME(psc_ptr);
  50.    len     = GET_LENGTH(psc_ptr);
  51.    /* check for potential heap overflow */
  52.    /*  (this will guarantee space for lists of up to 50 elements */
  53.    stack_top = (breg < ereg) ? breg : ereg - ENV_SIZE(cpreg);
  54.    if (stack_top < hreg + 100) {
  55.       /* garbage_collection("b_NAME0"); */
  56.       if (stack_top < hreg + 100)    /* still too full */
  57.      quit("Heap overflow\n");
  58.    }
  59.    if (len == 0) {
  60.      if (!unify(reg[2], nil_sym))
  61.        {FAIL0;}
  62.    }
  63.    else {
  64.        nlist = (LONG)hreg | LIST_TAG;
  65.        for (i = 0; i < len; i++) {
  66.         FOLLOW(hreg++) = MAKEINT(*name++);
  67.         top = hreg++;
  68.         FOLLOW(top) = (LONG)hreg | LIST_TAG;
  69.        }
  70.        FOLLOW(top) = nil_sym;
  71.        if (!unify(reg[2], nlist))
  72.            {FAIL0;}
  73.    }
  74. }  /* b_NAME0 */
  75.  
  76.  
  77. b_BLDATOM()  /* X, L: reg2 (L) is known not free, reg1 (X) is known free. */
  78. {            /* make X to be an atom with name string L       */
  79.  
  80.    WORD     a, n;
  81.    CHAR     name[256];
  82.    CHAR_PTR s;
  83.    register LONG     op2, op3;
  84.    register LONG_PTR top;
  85.  
  86.    s = name;
  87.    n = 0;
  88.    op2 = reg[2];
  89.  
  90.    do {
  91.       DEREF(op2);
  92.       if (op2 == nil_sym)
  93.      break;
  94.       if (ISLIST(op2)) {
  95.          UNTAG(op2);
  96.          op3 = FOLLOW(op2);  DEREF(op3);
  97.          if (!ISINTEGER(op3)) {
  98.             printf("Error: bldatom, non integer\n");
  99.             FAIL0;
  100.             return;
  101.          }
  102.          a = INTVAL(op3);
  103.          if (a < 0 || a > 255) {
  104.             printf("Error: bldatom, bad integer\n");
  105.             FAIL0;
  106.             return;
  107.          }
  108.          *s++ = a;
  109.          n++;
  110.          op2 = FOLLOW((op2+4));
  111.       } else {
  112.          printf("Error: bldstr, non list\n");
  113.          FAIL0;
  114.          return;
  115.       }
  116.    } while (1);
  117.    ptr = (LONG)insert(name, n, 0, &perm) | CS_TAG;
  118.  
  119.    if (!unify(reg[1], ptr))
  120.       {FAIL0;}
  121.  
  122. }  /* b_BLDATOM */
  123.